/////////////////////////////////////////////////////////////////////////////////////
// Header Protection
#ifndef _SINGLETON_H_
#define _SINGLETON_H_

template< class single_t >
class CSingleton
{
public:     // methods
	inline CSingleton() : m_ppInstance(&single_t::m_pInstance) { }

	inline single_t *operator ->()
	{
		assert(*m_ppInstance);
		return *m_ppInstance;
	}

private:    // data
	single_t **m_ppInstance;
};

/////////////////////////////////////////////////////////////////////////////////////
// Gives any inherited class singleton functionality.
// singleton_t: template - the type of the derived class
template< class single_t >
class ISingleton
{
private:   // friends
	friend class CSingleton<single_t>;

public:    // methods

	// Creates the instance of the singleton.
	static inline void CreateInstance()
	{
		assert(!m_pInstance);
		m_pInstance = new single_t();
	}

	// Destroys the instance of the singleton.
	static inline void DestroyInstance()
	{
		assert(m_pInstance);
		delete m_pInstance;
		m_pInstance = CE_NULL;
	}

protected:  // methods

	ISingleton() { }
	virtual ~ISingleton() = 0 { }

private:    // dummy definitions
	ISingleton( ISingleton<single_t> const & );
	ISingleton<single_t> & operator =( ISingleton<single_t> const & );

private:    // data
	static single_t *m_pInstance;
};

/////////////////////////////////////////////////////////////////////////////////////
// Header Protection
#endif // _SINGLETON_H_
